package com.ruoyi;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSON;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

/**
 * @author Tellsea
 * @date 2024−07−23
 */
public class QiniuMain {

    private static final String ACCESS_KEY = "jVSgpvZene6vFk4f3_KD4DZgTDGvKGnlILKai6In";
    private static final String SECRET_KEY = "NanAsKKJyaHgz7GqzTJWnQTaXfdQ7M0-G2lAKwrx";
    private static final String BUCKET_NAME = "health-management-sys";

    public static void main(String[] args) {
        try {
            Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
            String upToken = auth.uploadToken(BUCKET_NAME);
            System.out.println(upToken);
            Configuration cfg = new Configuration(Zone.zone0());
            cfg.upHost(upToken);
            UploadManager uploadManager = new UploadManager(cfg);
            byte[] bytes = FileUtil.readBytes("D:\\Workspace\\XunshuWorkspace\\engage\\engage-service\\ruoyi-admin\\src\\main\\test\\com\\ruoyi\\0c38460aa7ac9e8a550fd4ea9568aa6d.jpg");
            Response response = uploadManager.put(bytes, null, upToken);
            DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.toString());
            String imgName = putRet.key;
            System.out.println(imgName);
        } catch (QiniuException e) {
            throw new RuntimeException(e);
        }
    }
}
